﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;


namespace BusinessLayer.RegInfo
{
    public class MarketingBL
    {
        private readonly DbHelper _db;

        public MarketingBL()
        {
            _db = new DbHelper(new DbConfig().DbRegInfoConnectionString);
        }
        /// <summary>
        /// Get List of Marketing with full infomation
        /// </summary>
        /// <returns></returns>
        public List<MarketingFullInfo> GetAllFullInfoMarketings()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllFullInfoMarketing) { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<MarketingFullInfo>(oCommand);
            return list;
        }
        /// <summary>
        /// Create Marketing Discount
        /// </summary>
        /// <param name="discount"></param>
        /// <returns></returns>
        public int CreateMarketingDiscount(MarketingDiscount discount)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertMarketingDiscount) { CommandType = CommandType.StoredProcedure };

                oCommand.Parameters.Add(new SqlParameter("@Code", discount.Code));
                oCommand.Parameters.Add(new SqlParameter("@Title", discount.Title));
                oCommand.Parameters.Add(new SqlParameter("@MarketingCatID", discount.MarketingCatID));
                oCommand.Parameters.Add(new SqlParameter("@FromDate", discount.FromDate));
                oCommand.Parameters.Add(new SqlParameter("@ToDate", discount.ToDate));
                oCommand.Parameters.Add(new SqlParameter("@Status", discount.Status));
                oCommand.Parameters.Add(new SqlParameter("@SubscriptionID", discount.SubscriptionID));
                //oCommand.Parameters.Add(new SqlParameter("@CreatedAt", discount.CreatedAt));
                oCommand.Parameters.Add(new SqlParameter("@CreatedBy", discount.CreatedBy));
                oCommand.Parameters.Add(new SqlParameter("@Discount", discount.Discount));
                oCommand.Parameters.Add(new SqlParameter("@Contents", discount.Contents));

                result = _db.ExecuteNonQuery(oCommand);
                //Khong hieu tai sao thuc hien ok lai tra ra -1. Tam thoi fix la 1
                result = 1;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int CreateMarketingCoupon(MarketingCoupon coupon)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertMarketingCoupon) { CommandType = CommandType.StoredProcedure };

                oCommand.Parameters.Add(new SqlParameter("@Code", coupon.Code));
                oCommand.Parameters.Add(new SqlParameter("@Title", coupon.Title));
                oCommand.Parameters.Add(new SqlParameter("@MarketingCatID", coupon.MarketingCatID));
                oCommand.Parameters.Add(new SqlParameter("@FromDate", (object)coupon.FromDate));
                oCommand.Parameters.Add(new SqlParameter("@ToDate", (object)coupon.ToDate));
                oCommand.Parameters.Add(new SqlParameter("@Status", coupon.Status));
                oCommand.Parameters.Add(new SqlParameter("@SubscriptionID", coupon.SubscriptionID));
                //oCommand.Parameters.Add(new SqlParameter("@CreatedAt", (object)coupon.CreatedAt));
                oCommand.Parameters.Add(new SqlParameter("@CreatedBy", coupon.CreatedBy));
                oCommand.Parameters.Add(new SqlParameter("@Discount", coupon.CouponDiscount));
                oCommand.Parameters.Add(new SqlParameter("@CouponCode", coupon.CouponCode));
                oCommand.Parameters.Add(new SqlParameter("@Contents", coupon.Contents));

                result = _db.ExecuteNonQuery(oCommand);
               
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public int UpdateMarketingDiscount(MarketingDiscount discount)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateMarketingDiscount) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@MarketingID", discount.MarketingID));
                oCommand.Parameters.Add(new SqlParameter("@Code", discount.Code));
                oCommand.Parameters.Add(new SqlParameter("@Title", discount.Title));
                oCommand.Parameters.Add(new SqlParameter("@MarketingCatID", discount.MarketingCatID));
                oCommand.Parameters.Add(new SqlParameter("@FromDate", discount.FromDate));
                oCommand.Parameters.Add(new SqlParameter("@ToDate", discount.ToDate));
                oCommand.Parameters.Add(new SqlParameter("@Status", discount.Status));
                oCommand.Parameters.Add(new SqlParameter("@SubscriptionID", discount.SubscriptionID));

                oCommand.Parameters.Add(new SqlParameter("@LastUpdatedBy", discount.LastUpdatedBy));
                oCommand.Parameters.Add(new SqlParameter("@Discount", discount.Discount));
                oCommand.Parameters.Add(new SqlParameter("@Contents", discount.Contents));

                result = _db.ExecuteNonQuery(oCommand);
                
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateMarketingCoupon(MarketingCoupon coupon)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateMarketingCoupon) { CommandType = CommandType.StoredProcedure };

                oCommand.Parameters.Add(new SqlParameter("@MarketingID", coupon.MarketingID));
                oCommand.Parameters.Add(new SqlParameter("@Code", coupon.Code));
                oCommand.Parameters.Add(new SqlParameter("@Title", coupon.Title));
                oCommand.Parameters.Add(new SqlParameter("@MarketingCatID", coupon.MarketingCatID));
                oCommand.Parameters.Add(new SqlParameter("@FromDate", (object)coupon.FromDate));
                oCommand.Parameters.Add(new SqlParameter("@ToDate", (object)coupon.ToDate));
                oCommand.Parameters.Add(new SqlParameter("@Status", coupon.Status));
                oCommand.Parameters.Add(new SqlParameter("@SubscriptionID", coupon.SubscriptionID));
                oCommand.Parameters.Add(new SqlParameter("@LastUpdatedBy", coupon.LastUpdatedBy));
                oCommand.Parameters.Add(new SqlParameter("@Discount", coupon.CouponDiscount));
                oCommand.Parameters.Add(new SqlParameter("@CouponCode", coupon.CouponCode));
                oCommand.Parameters.Add(new SqlParameter("@Contents", coupon.Contents));

                result = _db.ExecuteNonQuery(oCommand);
               
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        /// <summary>
        /// Get full info of a marketing code
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public MarketingFullInfo GetFullInfoByCode(string code)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetFullInfoMarketingByCode) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Code", code));
            var list = _db.GetList<MarketingFullInfo>(oCommand);
            return list.FirstOrDefault();
        }
        public MarketingFullInfo GettFullInfoByMarketingID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetFullInfoMarketingByID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@MarketingID", id));
            var list = _db.GetList<MarketingFullInfo>(oCommand);
            return list.FirstOrDefault();
        }
        public int DeleteMarketingByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0, ids.Length - 1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var oCommand = new SqlCommand(String.Format("DELETE FROM Marketing WHERE MarketingID IN ({0});", ids)) { CommandType = CommandType.Text };
                    result = _db.ExecuteNonQuery(oCommand);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
    }
}
